ExposedMembers.AUS = ExposedMembers.AUS or {};
ExposedMembers.AUS.Utils = ExposedMembers.AUS.Utils or {};
AUSUtils = ExposedMembers.AUS.Utils;

local AUS_EXPLORER_INDEX = GameInfo.Units['UNIT_AUS_EXPLORER'].Index
local AUS_MINER_INDEX = GameInfo.Units['UNIT_AUS_MINER'].Index
local AUS_HERDER_INDEX = GameInfo.Units['UNIT_AUS_HERDER'].Index
local AUS_FISHERMAN_INDEX = GameInfo.Units['UNIT_AUS_FISHERMAN'].Index

function AUSImprovementAddedToMap(x, y, improvementId, playerId)
  local units = Units.GetUnitsInPlot(x, y)
  if units ~= nil then
    for _, unit in ipairs(units) do
      local unitType = unit:GetUnitType()
      if unitType == AUS_MINER_INDEX or unitType == AUS_HERDER_INDEX or unitType == AUS_FISHERMAN_INDEX then
        local param = {}
        param['OnStart'] = 'AUSUUBuiltImprovement'
        param['X'] = x
        param['Y'] = y
        UI.RequestPlayerOperation(playerId, PlayerOperations.EXECUTE_SCRIPT, param)
      end
    end
  end
end

-- 获取单位剩余移动力（包含小数）
function AUS_UI_GetUnitMovesRemaining(playerId, unitId)
  local unit = UnitManager.GetUnit(playerId, unitId);
  if unit then
    return unit:GetMovesRemaining();
  end
  return 0;
end
AUSUtils.GetUnitMovesRemaining = AUS_UI_GetUnitMovesRemaining

-- 获取同盟等级
function AUS_UI_GetAllianceType(playerId, alliesId)
  return Players[playerId]:GetDiplomacy():GetAllianceType(alliesId);
end
AUSUtils.GetAllianceType = AUS_UI_GetAllianceType

function AUS_UI_GetAllianceLevel(playerId, alliesId)
  return Players[playerId]:GetDiplomacy():GetAllianceLevel(alliesId);
end
AUSUtils.GetAllianceLevel = AUS_UI_GetAllianceLevel

--------------------------------------------------------------
-- Initialize
function initialize()
	Events.ImprovementAddedToMap.Add(AUSImprovementAddedToMap);
end
Events.LoadGameViewStateDone.Add(initialize);